/*
   Copyright 2009 Bloo. All Rights Reserved.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package org.youtensils.www.client.data.youtube;

import com.google.gwt.json.client.JSONObject;

/**
 * Represents a YouTube movie.
 * @author Bobby Soares
 */
public class YouTubeMovie {
	private String id, title, description, author, thumbnail, location;
	private int viewCount = 0, favoriteCount = 0, rating = 0, duration = 0;
	/**
	 * A constructor which extracts video information from GData JSON movie search result.
	 * @param obj a JSON object containing information for this movie
	 */
	public YouTubeMovie(JSONObject obj){
		this.id = YouTubeLoader.getStringAttribute(obj, new String[]{ "id", "$t" });
		if(this.id.indexOf(':') >= 0){
			this.id = this.id.substring(this.id.lastIndexOf(':') + 1);
		}
		this.author = YouTubeLoader.getStringAttribute(obj, new Object[]{ "author", 0, "name", "$t" });
		this.title = YouTubeLoader.getStringAttribute(obj, new String[]{ "title", "$t" });
		this.location = YouTubeLoader.getStringAttribute(obj, new Object[]{ "link", 0, "href" });
		this.description = YouTubeLoader.getStringAttribute(obj, new String[]{ "media$group", "media$description", "$t" });
		this.thumbnail = YouTubeLoader.getStringAttribute(obj, new Object[]{ "media$group", "media$thumbnail", 0, "url" });
		this.viewCount = (int)YouTubeLoader.getNumericAttribute(obj, new String[]{ "yt$statistics", "viewCount" });
		this.favoriteCount = (int)YouTubeLoader.getNumericAttribute(obj, new String[]{ "yt$statistics", "favoriteCount" });
		this.rating = (int)Math.round(YouTubeLoader.getNumericAttribute(obj, new String[]{ "gd$rating", "average" }));
		this.duration = (int)YouTubeLoader.getNumericAttribute(obj, new String[]{ "media$group", "yt$duration", "seconds" });
	}
	/**
	 * Retrieves a string representation of this movie's duration.
	 * @return this movie's duration
	 */
	public String getDurationLabel(){
		int minutes = Math.round(this.duration/60);
		int seconds = this.duration - (minutes*60);
		String mins = minutes < 10 ? "0" + minutes : "" + minutes;
		String secs = seconds < 10 ? "0" + seconds : "" + seconds;
		return mins + ":" + secs;
	}
	/**
	 * Retrieves a numeric representation of this movie's duration
	 * @return this movie's duration
	 */
	public int getDuration() {
		return duration;
	}
	/**
	 * Retrieves the number of views for this movie.
	 * @return this movie's view count
	 */
	public int getViewCount() {
		return viewCount;
	}
	/**
	 * Retrieves the view URL for this movie.
	 * @return this movie's URL
	 */
	public String getLocation() {
		return location;
	}
	/**
	 * Retrieves the number of times this movie has been added as favorite by users.
	 * @return the number of times this movie has been favorite'd
	 */
	public int getFavoriteCount() {
		return favoriteCount;
	}
	/**
	 * Retrieves this movie's user rating.
	 * @return this movie's user rating
	 */
	public int getRating() {
		return rating;
	}
	/**
	 * Retrieves this movie's id.
	 * @return this movie's id
	 */
	public String getId() {
		return id;
	}
	/**
	 * Retrieves this movie's title.
	 * @return this movie's title
	 */
	public String getTitle() {
		return title;
	}
	/**
	 * Retrieves this movie's description.
	 * @return this movie's description
	 */
	public String getDescription() {
		return description;
	}
	/**
	 * Retrieves this movie's description, truncated to a given length.
	 * If this maxLength value exceeds the length of the description, a truncated value will be returned.
	 * @param maxLength the maximum length for the description 
	 * @return this movie's description
	 */
	public String getDescription(int maxLength) {
		if(description.length() <= maxLength) return description;
		else return description.substring(0, maxLength) + "...";
	}
	/**
	 * Retrieves this movie's author.
	 * @return this movie's author
	 */
	public String getAuthor() {
		return author;
	}
	/**
	 * Retrieves this movie's thumbnail URL.
	 * @return this movie's thumbnail URL
	 */
	public String getThumbnail() {
		return thumbnail;
	}
	/**
	 * Retrieves this movie's embed URL using the default configuration.
	 * @return this movie's embed URL
	 */
	public String getEmbedUrl(){
		return getEmbedUrl(true, false, false, false, false, false, false, 0, null, null);
	}
	/**
	 * Retrieves this movie's embed URL, given a custom configuration.
	 * @param allowFullScreen whether to allow full screen mode
	 * @param showRelatedVideos whether related videos should be displayed at the end of playback
	 * @param autoPlay whether the movie should begin playing as soon as it loads
	 * @param enableJavasScript whether to allow JavaScript manipulation of the movie
	 * @param disableKeyboard whether keyboard support should be disabled
	 * @param showBorder whether a border should be shown around the movie
	 * @param showSearch whether to allow searching YouTube from within the movie player
	 * @param startIndex the index at which playback should start
	 * @param borderColor the border color
	 * @param backgroundColor the background color
	 * @return the embed URL
	 */
	public String getEmbedUrl(boolean allowFullScreen, boolean showRelatedVideos,
			boolean autoPlay, boolean enableJavasScript, boolean disableKeyboard,
			boolean showBorder, boolean showSearch, int startIndex,
			String borderColor, String backgroundColor){
		return "http://www.youtube.com/v/" + this.id +
			(allowFullScreen ? "&fs=1" : "") +
			(showRelatedVideos ? "&rel=1" : "") +
			(autoPlay ? "&autoplay=1" : "") +
			(enableJavasScript ? "&enablejsapi=1" : "") + 
			(disableKeyboard ? "&disablekb=1" : "") + 
			(showBorder ? "&border=1" : "") + 
			(showSearch ? "&showsearch=1" : "") + 
			(startIndex > 0 ? "&start=1" : "") + 
			(borderColor != null && borderColor != "" ? "&color1=" + borderColor : "") + 
			(backgroundColor != null && backgroundColor != "" ? "&color1=" + backgroundColor : "");
	}
	/**
	 * Retrieves this movie's embed HTML using the default configuration.
	 * @return this movie's embed HTML
	 */
	public String getEmbedHtml(){
		return getEmbedHtml(true, false, false, false, false, false, false, 0, null, null);
	}
	/**
	 * Retrieves this movie's embed HTML, given a custom configuration.
	 * @param allowFullScreen whether to allow full screen mode
	 * @param showRelatedVideos whether related videos should be displayed at the end of playback
	 * @param autoPlay whether the movie should begin playing as soon as it loads
	 * @param enableJavasScript whether to allow JavaScript manipulation of the movie
	 * @param disableKeyboard whether keyboard support should be disabled
	 * @param showBorder whether a border should be shown around the movie
	 * @param showSearch whether to allow searching YouTube from within the movie player
	 * @param startIndex the index at which playback should start
	 * @param borderColor the border color
	 * @param backgroundColor the background color
	 * @return the embed HTML
	 */
	public String getEmbedHtml(boolean allowFullScreen, boolean showRelatedVideos,
			boolean autoPlay, boolean enableJavasScript, boolean disableKeyboard,
			boolean showBorder, boolean showSearch, int startIndex,
			String borderColor, String backgroundColor){
		String url = getEmbedUrl(allowFullScreen, showRelatedVideos, autoPlay, enableJavasScript, disableKeyboard, showBorder, showSearch, startIndex, borderColor, backgroundColor);
		String html = "<object width=\"425\" height=\"344\">" +
			"<param name=\"movie\" value=\"" + url + "\"</param>" +
			(allowFullScreen ? "<param name=\"allowFullScreen\" value=\"true\"></param>" : "") +
			"<embed src=\"" + url + "\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" width=\"425\" height=\"344\">" +
			"</embed>" +
			"</object>";
		return html;
	}
}
